package com.hmdp.config;

import com.hmdp.utils.LoginInterceptor;
import com.hmdp.utils.RefreshTokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-01-25
 * Time: 16:10
 */

/**
 * 配置登陆拦截器->一定要加注解
 */
@Configuration
public class MVCConfig implements WebMvcConfigurer {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /**
         * excludePathPatterns : 排除一些登陆的请求-->不需要进行拦截的
         * 除了在登陆情况下发出的请求,剩下的都不需要进行拦截
         */
        //只拦截登陆才能做的事情的请求
        registry.addInterceptor(new LoginInterceptor())
                .excludePathPatterns(
                        "/user/login",
                        "/user/code",
                        "/blog/hot",
                        "/shop/**",
                        "/shop-type/**",
                        "/upload/**",
                        "/voucher/**"
                ).order(1);
        //order数字越大优先级越低

        //拦截所有请求  token刷新浏览器
        registry.addInterceptor(new RefreshTokenInterceptor
                (stringRedisTemplate)).addPathPatterns("/**").order(0);
    }
}